package main;

import bean.Student;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Description:
 * Created by 4790k on 2017/9/28.
 */
public class MainClass {
    public static void main(String[] args) {
        Thread thread1=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().getName()+" : "+" is running...");
                    //创建Student对象
                    Student st = new Student();
                    //使用CovertUtils注册创建一个日期格式转换器
                    ConvertUtils.register(new Converter() {
                        @Override
                        public Object convert(Class type, Object value) {
                            //当value参数等于空时返回空
                            if(value==null){
                                return null;
                            }
                            //自定义时间的格式为yyyy-MM-dd
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
                            //创建日期类对象
                            Date dt = null;
                            try {
                                //使用自定义日期的格式转化value参数为yyyy-MM-dd格式
                                dt = sdf.parse((String)value);
                            } catch (ParseException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            //返回dt日期对象
                            return dt;
                        }
                    }, Date.class);
                    //给birth赋值
                    BeanUtils.setProperty(st, "birthday", "1991-09-25");
                    //输出
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
                    System.out.println(Thread.currentThread().getName()+" : "+sdf.format(st.getBirthday()));
                    System.out.println(Thread.currentThread().getName()+" : "+st.getBirthday());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+" : "+"is ending...");
            }
        });
        Thread thread2=new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Thread2 is beginning...");
                Student student1=new Student();
                //System.out.println(student1.getBirthday());
                try {
                    BeanUtils.setProperty(student1,"birthday","2014-4-30");
                    System.out.println(Thread.currentThread().getName()+" : "+student1.getBirthday());
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread2 is ending...");
            }
        });
        thread1.start();
        thread2.start();
        System.out.println("end");
    }
}
